package com.huslee.shark.alert.handler;

import com.huslee.shark.alert.config.DingtalkConfigs;
import com.huslee.shark.alert.entity.AlertInfo;
import com.huslee.shark.alert.enums.AlertTypeEnum;
import com.huslee.shark.alert.exception.AlertException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @Desc
 * @Author lishuai
 * @Create 2021/1/12
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DingtalkAlerter {

    private Boolean asyn;
    private DingtalkConfigs.DingtalkConfig dingtalkConfig;
    private List<String> blackUsers;


    public AlertException alertTextException(String blackUser, List<String> ats, List<String> contents){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.TEXT.getType(), ats, null, contents, null, null, null);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public AlertException alertTextException(String blackUser, List<String> ats, List<String> contents, Exception e){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.TEXT.getType(), ats, null, contents, null, null, e);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public void alertText(String blackUser, List<String> ats, List<String> contents){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.TEXT.getType(), ats, null, contents, null, null, null);
        execute(blackUser, alertInfo);
    }
    public void alertText(String blackUser, List<String> ats, List<String> contents, Exception e){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.TEXT.getType(), ats, null, contents, null, null, e);
        execute(blackUser, alertInfo);
    }



    public AlertException alertLinkException(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.LINK.getType(), ats, title, contents, messageUrl, null, null);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public AlertException alertLinkException(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl, String picUrl){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.LINK.getType(), ats, title, contents, messageUrl, picUrl, null);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public AlertException alertLinkException(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl, Exception e){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.LINK.getType(), ats, title, contents, messageUrl, null, e);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public AlertException alertLinkException(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl, String picUrl, Exception e){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.LINK.getType(), ats, title, contents, messageUrl, picUrl, e);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public void alertLink(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.LINK.getType(), ats, title, contents, messageUrl, null, null);
        execute(blackUser, alertInfo);
    }
    public void alertLink(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl, String picUrl){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.LINK.getType(), ats, title, contents, messageUrl, picUrl, null);
        execute(blackUser, alertInfo);
    }
    public void alertLink(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl, Exception e){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.LINK.getType(), ats, title, contents, messageUrl, null, e);
        execute(blackUser, alertInfo);
    }
    public void alertLink(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl, String picUrl, Exception e){
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.LINK.getType(), ats, title, contents, messageUrl, picUrl, e);
        execute(blackUser, alertInfo);
    }



    public AlertException alertMarkdownException(String blackUser, List<String> ats, String title, List<String> contents) {
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.MARKDOWN.getType(), ats, title, contents, null, null, null);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public AlertException alertMarkdownException(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl) {
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.MARKDOWN.getType(), ats, title, contents, messageUrl, null, null);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public AlertException alertMarkdownException(String blackUser, List<String> ats, String title, List<String> contents, Exception e) {
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.MARKDOWN.getType(), ats, title, contents, null, null, e);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public AlertException alertMarkdownException(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl, Exception e) {
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.MARKDOWN.getType(), ats, title, contents, messageUrl, null, e);
        execute(blackUser, alertInfo);
        return new AlertException(alertInfo);
    }
    public void alertMarkdown(String blackUser, List<String> ats, String title, List<String> contents) {
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.MARKDOWN.getType(), ats, title, contents, null, null, null);
        execute(blackUser, alertInfo);
    }
    public void alertMarkdown(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl) {
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.MARKDOWN.getType(), ats, title, contents, messageUrl, null, null);
        execute(blackUser, alertInfo);
    }
    public void alertMarkdown(String blackUser, List<String> ats, String title, List<String> contents, Exception e) {
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.MARKDOWN.getType(), ats, title, contents, null, null, e);
        execute(blackUser, alertInfo);
    }
    public void alertMarkdown(String blackUser, List<String> ats, String title, List<String> contents, String messageUrl, Exception e) {
        AlertInfo alertInfo = new AlertInfo(AlertTypeEnum.MARKDOWN.getType(), ats, title, contents, messageUrl, null, e);
        execute(blackUser, alertInfo);
    }


    private void execute(String blackUser, AlertInfo alertInfo) {
        try{
            if(StringUtils.isNotBlank(blackUser) && CollectionUtils.isNotEmpty(blackUsers) && blackUsers.contains(blackUser)){
                return;
            }
            if(this.asyn){
                RequestControl.exec(alertInfo, dingtalkConfig.getWebhook(), dingtalkConfig.getSecret());
            }else{
                RequestControl.execSyn(alertInfo, dingtalkConfig.getWebhook(), dingtalkConfig.getSecret());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }




}
